#include "stdafx.h"

#include "bochs.h"
#include "softfloat-specialize.h"

void IA32_CPU::FPU_stack_overflow(void)
{
  if ( the_i387.is_IA_masked())
  {
       the_i387.FPU_push();
      IA32_WRITE_FPU_REGISTER_AND_TAG(floatx80_default_nan, FPU_Tag_Special, 0);
  }
  FPU_exception(FPU_EX_Stack_Overflow);
}

void IA32_CPU::FPU_stack_underflow(int stnr, int pop_stack)
{
  if ( the_i387.is_IA_masked())
  {
     IA32_WRITE_FPU_REGISTER_AND_TAG(floatx80_default_nan, FPU_Tag_Special, stnr);
     if (pop_stack) 
           the_i387.FPU_pop();
  }
  FPU_exception(FPU_EX_Stack_Underflow);
}

int IA32_CPU::FPU_exception(int exception)
{
  int unmasked = 0;

  /* Extract only the bits which we use to set the status word */
  exception &= (FPU_SW_Exceptions_Mask);
  /* Set the corresponding exception bits */
  FPU_PARTIAL_STATUS |= exception;
  /* Set summary bits iff exception isn't masked */
  if (FPU_PARTIAL_STATUS & ~FPU_CONTROL_WORD & FPU_CW_Exceptions_Mask)
  {
      FPU_PARTIAL_STATUS |= (FPU_SW_Summary | FPU_SW_Backward);
      unmasked = 1;
  }

  if (exception & (FPU_SW_Stack_Fault | FPU_EX_Precision))
  {
      if (! (exception & FPU_SW_C1))
        /* This bit distinguishes over- from underflow for a stack fault,
             and roundup from round-down for precision loss. */
        FPU_PARTIAL_STATUS &= ~FPU_SW_C1;
  }
  return unmasked;
}

